#include "glutInputHandler.h"
#include <Windows.h>
#include <gl/glut.h>

//-------------------------------------------- inicjalizujemy globalne zmienne ---------------------------------------------//
bool GlutInputHandler::keyboardKeys[256];		
bool GlutInputHandler::specialKeyboardKeys[25];
bool GlutInputHandler::mouseKeys[3];	
int GlutInputHandler::mouseX = 0;
int GlutInputHandler::mouseY = 0;
unsigned char GlutInputHandler::keyPressed = NULL;
//int GlutInputHandler::numberOffPressedKeysCounter = 0;
//---------------------------------------------- initialize ----------------------------------------------------------------//
void GlutInputHandler::initialize()
{
	for( int ii = 0; ii < 256; ++ii )
		keyboardKeys[ii] = false;
	for(int ii =0; ii < 3; ++ii)
		mouseKeys[ii] = false;
	for( int ii = 0; ii < 110; ++ii )
		specialKeyboardKeys[ii] = false;
}
//---------------------------------------------- normalKeyboardKeysDownCallback --------------------------------------------//
void GlutInputHandler::normalKeyboardKeysDownCallback(unsigned char key, int x, int y)
{
	keyboardKeys[key] = true;
	//++numberOffPressedKeysCounter;
	keyPressed = key;
}
//---------------------------------------------- normalKeyboardKeysUpCallback ----------------------------------------------//
void GlutInputHandler::normalKeyboardKeysUpCallback(unsigned char key, int x, int y)
{
	keyboardKeys[key] = false;
	//--numberOffPressedKeysCounter;
	keyPressed = NULL;
}
//---------------------------------------------- specialKeyboardKeysDownCallback -------------------------------------------//
void GlutInputHandler::specialKeyboardKeysDownCallback( int key, int x, int y )
{
	if( key <= 12 )
		specialKeyboardKeys[key-1] = true;
	else
		specialKeyboardKeys[key-88] = true;
}
//---------------------------------------------- specialKeyboardKeysUpCallback ---------------------------------------------//
void GlutInputHandler::specialKeyboardKeysUpCallback( int key, int x, int y )
{
	if( key <= 12 )
		specialKeyboardKeys[key-1] = false;
	else
		specialKeyboardKeys[key-88] = false;
}
//---------------------------------------------- mouseCallback -------------------------------------------------------------//
void GlutInputHandler::mouseCallback( int button, int state, int x, int y )
{
	if( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN )
		mouseKeys[0] = true;
	if( button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN )
		mouseKeys[1] = true;
	if( button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN )
		mouseKeys[2] = true;

	if( button == GLUT_LEFT_BUTTON && state == GLUT_UP )
		mouseKeys[0] = false;
	if( button == GLUT_MIDDLE_BUTTON && state == GLUT_UP )
		mouseKeys[1] = false;
	if( button == GLUT_RIGHT_BUTTON && state == GLUT_UP )
		mouseKeys[2] = false;
}
//---------------------------------------------- glutMotionCallback -----------------------------------//
void GlutInputHandler::glutMotionCallback( int x, int y )
{
	mouseX = x;
	mouseY = y;
}
//---------------------------------------------- glutPassiveMotionCallback ----------------------------//
void GlutInputHandler::glutPassiveMotionCallback( int x, int y )
{
	mouseX = x;
	mouseY = y;
}
//---------------------------------------------- getMouseX --------------------------------------------//
const int GlutInputHandler::getMouseX()
{ return mouseX; }
//---------------------------------------------- getMouseY --------------------------------------------//
const int GlutInputHandler::getMouseY()
{ return mouseY; }
//---------------------------------------------- setKeyPressed ----------------------------------------//
void GlutInputHandler::setKeyPressed( unsigned char ch )
{ keyPressed = ch; }
//---------------------------------------------- isAnyKeyPressed --------------------------------------//
const bool GlutInputHandler::isAnyKeyPressed()
{
	if( keyPressed == NULL )
		return false;
	else
		return true;
}
//---------------------------------------------- getKeyPressed----------------------------------------//
const unsigned char GlutInputHandler::getKeyPressed()
{
	return keyPressed;
}
//---------------------------------------------- isKeyPressed -----------------------------------------//
const bool GlutInputHandler::isKeyPressed( const unsigned char key )
{ return keyboardKeys[key]; }

//------------------------------------------ setKey ---------------------------------------------------//
void GlutInputHandler::setKey( const unsigned char key, const bool state )
{ keyboardKeys[key] = state; }

//------------------------------------------ isSpecialKeyPressed --------------------------------------//
const bool GlutInputHandler::isSpecialKeyPressed( int glutFlag )
{
	if( glutFlag <= 12 )
		return specialKeyboardKeys[ glutFlag-1 ];
	else
		return specialKeyboardKeys[ glutFlag-88 ];
}

//------------------------------------------ setSpecialKey --------------------------------------------//
void GlutInputHandler::setSpecialKey( const int glutFlag, const bool state )
{
	if( glutFlag <= 12 )
		specialKeyboardKeys[ glutFlag-1 ] = state;
	else
		specialKeyboardKeys[ glutFlag-88 ] = state;
}

//------------------------------------------ isAltPressed ---------------------------------------------//
const bool GlutInputHandler::isAltPressed()
{ return ((GetKeyState( VK_MENU ) & 0x80) > 0); }

//------------------------------------------ isCtrlPressed --------------------------------------------//
const bool GlutInputHandler::isCtrlPressed()
{ return ((GetKeyState( VK_CONTROL ) & 0x80) > 0); }

//------------------------------------------ isShiftPressed -------------------------------------------//
const bool GlutInputHandler::isShiftPressed()
{ return ((GetKeyState( VK_SHIFT ) & 0x80) > 0); }

//------------------------------------------ isLeftMouseBtnPressed ------------------------------------//
const bool GlutInputHandler::isLeftMouseBtnPressed()
{ return mouseKeys[0]; }

//----------------------------------------- idRightMouseBtnPressed ------------------------------------//
const bool GlutInputHandler::isRightMouseBtnPressed()
{ return mouseKeys[2]; }

//----------------------------------------- isMiddleMouseBtnPressed -----------------------------------//
const bool GlutInputHandler::isMiddleMouseBtnPressed()
{ return mouseKeys[1]; }

//----------------------------------------- setLeftMouseBtn -------------------------------------------//
void GlutInputHandler::setLeftMouseBtn( const bool state )
{ mouseKeys[0] = state; }

//----------------------------------------- setMiddleMouseBtn -----------------------------------------//
void GlutInputHandler::setMiddleMouseBtn( const bool state )
{ mouseKeys[1] = state; }

//----------------------------------------- setRightMouseBtn ------------------------------------------//
void GlutInputHandler::setRighttMouseBtn( const bool state )
{ mouseKeys[2] = state; }